package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.util.SwitchEnum;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.SeedAddrRequest;
import com.frogs.ucenter.v1.request.SeedPayRequest;
import com.frogs.ucenter.v1.request.SeedRequest;
import com.frogs.ucenter.v1.request.UserShippingAddressAddRequest;
import com.frogs.ucenter.v1.response.DefaultResponse;
import com.frogs.ucenter.v1.service.SeedUserInfoService;
import com.frogs.ucenter.v1.service.UserShippingAddressService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Controller(ApiConstants.VERSION + "seedUserInfoController")
@RequestMapping("/" + ApiConstants.VERSION)
public class SeedUserInfoController extends BaseController3 {

	private static final String ROOT = "/seedUserInfo";

	private static final String SU_ADD = ROOT + "/add";

	private static final String SHIP_ADRESS_ADD = ROOT + "/addShipAddr";
	
	private static final String PAY_ADD=ROOT + "/addPay";

	@Resource(name = ApiConstants.VERSION + "seedUserInfoService")
	private SeedUserInfoService seedUserInfoService;

	@Resource(name = ApiConstants.VERSION + "userShippingAddressService")
	private UserShippingAddressService userShippingAddressService;

	@RequestMapping(SU_ADD)
	public Response suAdd(HttpServletRequest request, ModelMap model) {
		Response response = new DefaultResponse();
		try {
			SeedRequest req = new SeedRequest();
			req.parse(request);

			response = seedUserInfoService.addSeedUser(req);

		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;

	}

	@RequestMapping(SHIP_ADRESS_ADD)
	public Response shipAdd(HttpServletRequest request, ModelMap model) {
		Response response = new DefaultResponse();
		try {
			SeedAddrRequest req=new SeedAddrRequest();
			req.parse(request);
			

			UserShippingAddressAddRequest addReq = new UserShippingAddressAddRequest();
			addReq.parse(request);
			addReq.setIsDefault(SwitchEnum.YES.getId());
			

			seedUserInfoService.addSeedAddr(req);
			userShippingAddressService.insert(addReq);

		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(PAY_ADD)
	public Response payAdd(HttpServletRequest request, ModelMap model) {
		Response response = new DefaultResponse();
		try {
			SeedPayRequest req = new SeedPayRequest();
			req.parse(request);
			
			seedUserInfoService.addSeedPay(req);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
			
}
